package org.tc.shopmall.service.impl;

import Dto.OrdersDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.tc.shopmall.entity.Orderdetails;
import org.tc.shopmall.entity.Orders;
import org.tc.shopmall.mapper.OrderdetailsMapper;
import org.tc.shopmall.mapper.OrdersMapper;
import org.tc.shopmall.service.OrderdetailService;

import java.util.List;
import java.util.Random;

@RestController
public class OrderdetailsServiceImpl implements OrderdetailService {

    @Autowired
    private OrderdetailsMapper orderdetailsMapper;

    @Autowired
    private OrdersMapper ordersMapper;

//    @Override
//    public int insert(Orderdetails record) {
//        return orderdetailsMapper.insertDetail(record);
//    }
     public int orderCode;   //订单号

    @Override
    public int insert(@RequestBody Orderdetails record) {
        return orderdetailsMapper.insertDetail(record);
    }

    @Override
    public int insertOrder(@RequestBody OrdersDto ordersDto) {
        Random rand = new Random();
        int orderCode1=0;
        int orderCode2=0;
        int orderCode3=0;
        orderCode1=rand.nextInt(10000) + 1;
        orderCode2=rand.nextInt(10000) + 1;
        orderCode3=rand.nextInt(10000) + 1;
        orderCode=orderCode1+orderCode2+orderCode3;
        System.out.println("随机数1="+orderCode1);
        System.out.println("随机数2="+orderCode2);
        System.out.println("随机数3="+orderCode3);
        System.out.println("最终随机数="+orderCode);
        Orders orders = new Orders();
        orders.setOrderid(orderCode);
        BeanUtils.copyProperties(ordersDto, orders);
        int save = ordersMapper.insert(orders);
        if (save > 0) {
            for (Orderdetails orderdetails : ordersDto.getList()) {
                orderdetails.setOrderid(orderCode);
                save = orderdetailsMapper.insertDetail(orderdetails);
            }
        }
        return save;
    }

    @Override
    public Integer selectOneOrderId() {
        System.out.println(orderCode);
        return orderCode;
    }

    @Override
    public List<Orderdetails> selectAll() {
        return orderdetailsMapper.selectAll();
    }


    @Override
    public int deleteById(Integer id, Integer commodityid) {
        return orderdetailsMapper.deleteById(id, commodityid);
    }

    @Override
    public Orderdetails selectById(Integer id) {
        return orderdetailsMapper.selectById(id);
    }
}
